﻿using System.Linq;
using System.Xml;
using DataScripter.Scripting;
using DataScripter.Scripting.InputSources;
using DataScripter.Scripting.Output;
using DataScripter.Scripting.OutputDestinations;
using DataScripter.Scripting.ScriptDefinitions;
using DataScripter.Scripting.ScriptRequests;
using DataScripter.XmlInterpreter;
using DataScripter.XmlInterpreter.XmlInterpreters;

namespace DataScripter.UI.Old
{
// ReSharper disable UnusedMember.Global
    class XmlEngineConfiguratorv3 : IXmlEngineConfigurator
// ReSharper restore UnusedMember.Global
    {
        public IEngine Configure(IEngine engine, XmlDocument document, ScriptOptions scriptOptions)
        {
            XmlElement script = document.DocumentElement;
            if (script == null)
            {
                return engine;
            }

            scriptOptions.ScriptOutputType = ScriptOutputType.File;

            foreach (XmlElement element in script.ChildNodes.OfType<XmlElement>())
            {
                switch (element.Name)
                {
                    case "InputSource":
                        IInputSource source = new InputSourceInterpreter(engine).InterpretElement(element);
                        engine.RegisterType(source.Name, source);
                        break;
                    case "Output":
                        IOutputDestination destination = new OutputDestinationInterpreter(engine).InterpretElement(element);
                        engine.RegisterType(destination.Name, destination);
                        break;
                    case "ScriptRequest":
                        IScriptRequest scriptRequest = new ScriptRequestInterpreter().InterpretElement(element);
                        engine.RegisterType(scriptRequest.Name, scriptRequest);
                        break;
                    case "ScriptDefinition":
                        IScriptDefinition scriptDefinition = new ScriptDefinitionInterpreter(engine).InterpretElement(element);
                        engine.RegisterType(scriptDefinition.Name, scriptDefinition);
                        break;
                }
            }

            scriptOptions.Filename = ((IFileOutputDestination)engine.Resolve<IOutputDestination>()).OutputPath;

            return engine;
        }
    }
}